<template>
  <div class="subject">
    <span>{{ num1 }}</span>
    <span>+</span>
    <span>{{ num2 }}</span>
    <span>=</span>
    <input type="number" v-model.number="num" />
    <button :data-id="item" @click="add(item)">提交</button>
  </div>
</template>

<script>
export default {
  props: ['item'],
  data() {
    return {
      num1: Math.floor(Math.random() * 20 + 1),
      num2: Math.floor(Math.random() * 20 + 1),
      num: '',

    }
  },
  computed: {
    sum() {
      return this.num1 + this.num2
    }
  },
  methods: {
    // 判断用户是否以作答
    add(id) {
      if (this.num === '') {
        alert('还未作答,请先作答')
        this.$emit('succed', [id, '未完成'])
      }
      else if (this.num === this.sum) {
        // console.log(id);
        alert(`第${id}题,回答正确`)
        this.$emit('succed', [id, '回答正确'])
      } else {
        alert(`第${id}题,回答错误`)
        this.$emit('succed', [id, '回答错误'])
      }
    }
  }
};
</script>

<style scoped>
.subject {
  margin: 5px;
  padding: 5px;
  font-size: 20px;
}

.subject span {
  display: inline-block;
  text-align: center;
  width: 20px;
}

.subject input {
  width: 50px;
  height: 20px;
}
</style>